import { OccupyFee } from "@/pages/station/occupy/components/OccupyFee"
import dayjs, { Dayjs } from "dayjs"
import { history } from '@umijs/max'

export const jump2target = (path: string, options?: {name?: string, params?: any, needRefresh?: boolean}) => {
  console.log(options)
  if (options?.name) {
    sessionStorage.setItem(`__${path.split('?')[0]}_title`, options?.name)
  }
  if (options?.params) {
    window.__tempParams[path.split('?')[0]] = options.params
  }
  if (options?.needRefresh) {
    sessionStorage.setItem('needRefresh', 'yes')
  }
  history.push(path)
}

export const getJumpParams = () => {
  const params = window.__tempParams[history.location.pathname]
  window.__tempParams[history.location.pathname] = undefined
  return params
}

export function zeroFill(number: number, length: number = 2) {
  function fill(size: number) {
    return '0'.repeat(size) + number
  }
  if (number === null || number === undefined) {
    return fill(length)
  }
  const str = String(number)
  return str.length >= length ? str : fill(length - str.length)
}

export const areaCode2cascaderValue = (areaCode: string) => {
  if (!areaCode || areaCode.length !== 6) return undefined
  if (areaCode.endsWith('0000')) return [areaCode]
  if (areaCode.endsWith('00')) return [`${areaCode.substring(0, 2)}0000`, areaCode]
  return [`${areaCode.substring(0, 2)}0000`, `${areaCode.substring(0, 4)}00`, areaCode]
}

export const minute2timeObj = (minute: number) => {
  return {
    hour: Math.floor(minute / 60),
    minute: minute % 60,
  }
}

export const minutes2dayjs = (minutes?: number) => {
  if (minutes === undefined) return undefined
  const hour = Math.floor(minutes / 60)
  const minute = minutes % 60
  return dayjs().set('hour', hour).set('minute', minute)
}

export const dayjs2minutes = (day?: Dayjs) => {
  if (!day) return 0
  const hour = day.hour()
  const minute = day.minute()
  return hour * 60 + minute
}

export const parseOccupyFee = (feeInfo: string): OccupyFee[] => {
  if (!feeInfo) return []
  const feeList = JSON.parse(feeInfo)
  if (!Array.isArray(feeList)) return []
  let startTime = 0
  return feeList.map(item => {
    const [endTime, price] = item
    const _startTime = startTime
    const _endTime = (Math.floor(endTime / 100) * 60) + (endTime % 100)
    startTime = _endTime + 1
    return {
      startTime: _startTime,
      endTime: _endTime,
      price,
      fakeId: _startTime,
    }
  })
}

export const formatDiscount = (value: number) => {
  // if (value % 10) return value
  return value / 10
}

export const parseAdvWeekInfo = (weekInfo: string) => {
  if (!weekInfo) return ''
  const weeks: string[] = []
  if (weekInfo.includes('2')) {
    weeks.push('周一')
  }
  if (weekInfo.includes('3')) {
    weeks.push('周二')
  }
  if (weekInfo.includes('4')) {
    weeks.push('周三')
  }
  if (weekInfo.includes('5')) {
    weeks.push('周四')
  }
  if (weekInfo.includes('6')) {
    weeks.push('周五')
  }
  if (weekInfo.includes('7')) {
    weeks.push('周六')
  }
  if (weekInfo.includes('1')) {
    weeks.push('周日')
  }
  return `每 ${weeks.join('、')} 推送一次`
}

export const stringifyOccupyFee = (feeList: Occupy.Fee[]): string => {
  const strList = feeList.map(item => {
    const endTime = minute2timeObj(item.endTime)
    return [endTime.hour * 100 + endTime.minute, item.price]
  })
  return JSON.stringify(strList)
}

export const reorder = (list: Array<any>, startIndex: number, endIndex: number) => {
  const result = [...list]
  const [removed] = result.splice(startIndex, 1)
  result.splice(endIndex, 0, removed)
  return result
}
